package com.infomanage.service.study;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.PersonDAO;
import com.infomanage.dao.StudyPhysicalDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.Person;
import com.infomanage.po.StudyPhysical;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.StudyPhysicalItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class StudyPhysicalService {

	public static StudyPhysicalItem loadStudyPhysical(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId= request.getParameter("itemId");
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			if(StringUtils.isEmpty(itemId)) return null;
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<StudyPhysicalItem> loadStudyPhysicalList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			List<StudyPhysicalItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveStudyPhysical(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			StudyPhysical study = new StudyPhysical();
			study.setDescription(request.getParameter("description"));
			study.setFuzhong(Integer.parseInt(request.getParameter("fuzhong")));
			study.setJuzhong(Integer.parseInt(request.getParameter("juzhong")));
			study.setLali(Integer.parseInt(request.getParameter("lali")));
			study.setPaobuMin(request.getParameter("paobuMin"));
			study.setPaobuSec(request.getParameter("paobuSec"));
			study.setPasheng(request.getParameter("pasheng"));
			study.setPersonId(Integer.parseInt(request.getParameter("personId")));
			study.setTiaogao(Integer.parseInt(request.getParameter("tiaogao")));
			study.setTiaoyuan(request.getParameter("tiaoyuan"));
			study.setTimestamp(request.getParameter("timestamp"));
			study.setYalingMid(request.getParameter("yalingMid"));
			study.setYalingUp(request.getParameter("yalingUp"));
			study.setYalingBot(request.getParameter("yalingBot"));
			study.setYinti(Integer.parseInt(request.getParameter("yinti")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				study.setDeptId(person.getDeptId());
			}
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			dao.add(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeStudyPhysical(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String studyId = request.getParameter("itemId");
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			dao.remove(StudyPhysical.class, Integer.parseInt(studyId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeBatchStudyPhysical(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(StudyPhysical.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject modifyStudyPhysical(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("itemId"));
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			StudyPhysical study = dao.find(StudyPhysical.class, planId);
			study.setDescription(request.getParameter("description"));
			study.setFuzhong(Integer.parseInt(request.getParameter("fuzhong")));
			study.setJuzhong(Integer.parseInt(request.getParameter("juzhong")));
			study.setLali(Integer.parseInt(request.getParameter("lali")));
			study.setPaobuMin(request.getParameter("paobuMin"));
			study.setPaobuSec(request.getParameter("paobuSec"));
			study.setPasheng(request.getParameter("pasheng"));
			study.setPersonId(Integer.parseInt(request.getParameter("personId")));
			study.setTiaogao(Integer.parseInt(request.getParameter("tiaogao")));
			study.setTiaoyuan(request.getParameter("tiaoyuan"));
			study.setTimestamp(request.getParameter("timestamp"));
			study.setYalingMid(request.getParameter("yalingMid"));
			study.setYalingUp(request.getParameter("yalingUp"));
			study.setYalingBot(request.getParameter("yalingBot"));
			study.setYinti(Integer.parseInt(request.getParameter("yinti")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				study.setDeptId(person.getDeptId());
			}
			dao.modify(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static List<StudyPhysicalItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<StudyPhysicalItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			StudyPhysicalDAO dao = new StudyPhysicalDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String timestamp = request.getParameter("timestamp");
		String personId = request.getParameter("personId");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(timestamp)){
			FieldBean field = new FieldBean("timestamp", timestamp);
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(personId)){
			FieldBean field = new FieldBean("personId", personId);
			searchBean.add(field);
		}
		
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
